package com.wuliu.servlet;

import com.wuliu.dao.BusinessClassDAO;
import com.wuliu.dao.BusinessDAO;
import com.wuliu.dao.impl.BusinessClassDAOImpl;
import com.wuliu.dao.impl.BusinessDAOImpl;
import com.wuliu.model.Business;
import com.wuliu.model.BusinessClass;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/page3")
public class BusinessServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        BusinessClassDAO businessClassDAO= new BusinessClassDAOImpl();
        List<BusinessClass> businessClasses =businessClassDAO.findAll();
        req.setAttribute("businessClasses",businessClasses);

        BusinessDAO businessDAO =new BusinessDAOImpl();
        String cid =req.getParameter("businessClassId");
        if (cid == null) {
            List<Business> businesses =businessDAO.findAll();
            req.setAttribute("businesses",businesses);

        }else {
            List<Business>businesses =businessDAO.findByCid(Integer.valueOf(cid));
            req.setAttribute("businesses",businesses);
        }

        req.getRequestDispatcher("/WEB-INF/view/business.jsp").forward(req,resp);
    }

}
